package com.enwei.activitys;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;

import com.enwei.adapters.OrdersAdapter;
import com.enwei.adapters.VideoListAdapter;
import com.enwei.base.BaseActivity;
import com.enwei.utils.CacheKeys;
import com.enwei.utils.Constans;
import com.enwei.utils.SpfHelper;
import com.enwei.utils.http.HttpApi;
import com.enwei.utils.http.HttpCallBack;
import com.ew.action.R;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class OrdersActivity extends BaseActivity{
	
	private ListView mListView;
	private OrdersAdapter mAdapter;
	private JSONArray arrays = new JSONArray();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_orders,true,true);
		mListView = (ListView) findViewById(R.id.ordersList);
		initData();
	}

	public void initData(){
		mAdapter = new OrdersAdapter(arrays, this);
		mListView.setAdapter(mAdapter);
		requestData();
	}
	
	public void requestData(){
		String url = Constans.PRODUCT_ORDER;
		String phone = (String) SpfHelper.getParam(this, CacheKeys.LOGIN_USER_HOME, "");
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		nameValuePairs.add(new BasicNameValuePair("phone", phone));
		HttpApi http = new HttpApi(this,HttpMethod.POST);
		http.sendHttpRequest(url, nameValuePairs, new HttpCallBack() {
			
			@Override
			public void onSuccess(String data) {
				try {
					getLoadingView().setVisibility(View.GONE);
					if(!TextUtils.isEmpty(data)){
						arrays = new JSONArray(data);
						mAdapter.updateView(arrays);
						mListView.setVisibility(View.VISIBLE);
					}else{
						getEmpty().setVisibility(View.VISIBLE);
					}
				} catch (JSONException e) {
					getErrorLayout().setVisibility(View.VISIBLE);
					e.printStackTrace();
				}
				
			}
			@Override
			public void onFailure() {
				getLoadingView().setVisibility(View.GONE);
				getErrorLayout().setVisibility(View.VISIBLE);
				super.onFailure();
			}
		});
	}
	
	@Override
	protected void onRetry() {
		mListView.setVisibility(View.GONE);
		getLoadingView().setVisibility(View.VISIBLE);
		getErrorLayout().setVisibility(View.GONE);
		requestData();
		super.onRetry();
	}
	
}